import 'dart:io';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../assets/color_config.dart';
import '../common/common_config.dart';
import 'button_widget.dart';

class FlutterDialog {
  static show(
    BuildContext context, {
    String? title,
    Widget? contentWidget,
    String? content,
    bool? barrierDismissible,
    EdgeInsets? contentPadding,
    String? cancel,
    String? ok,
    Color? cancelColor,
    Color? okColor,
    Function? cancelFun,
    Function? okFun,
  }) {
    if (Platform.isIOS || Platform.isAndroid) {
      cupertinoDialog(
        context,
        title: title,
        contentWidget: contentWidget,
        content: content,
        barrierDismissible: barrierDismissible,
        cancel: cancel,
        ok: ok,
        cancelColor: cancelColor,
        okColor: okColor,
        cancelFun: cancelFun,
        okFun: okFun,
      );
    } else {
      simpleDialog(
        context,
        title: title,
        contentWidget: contentWidget,
        content: content,
        barrierDismissible: barrierDismissible,
        contentPadding: contentPadding,
        cancel: cancel,
        ok: ok,
        cancelColor: cancelColor,
        okColor: okColor,
        cancelFun: cancelFun,
        okFun: okFun,
      );
    }
    //废弃安卓形式弹窗
    // alertDialog(
    //   context,
    //   title: title,
    //   contentWidget: contentWidget,
    //   content: content,
    //   barrierDismissible: barrierDismissible,
    //   contentPadding: contentPadding,
    //   cancel: cancel,
    //   ok: ok,
    //   cancelColor: cancelColor,
    //   okColor: okColor,
    //   cancelFun: cancelFun,
    //   okFun: okFun,
    // );
  }

  static cupertinoDialog(
    BuildContext context, {
    String? title,
    Widget? contentWidget,
    String? content,
    bool? barrierDismissible,
    String? cancel,
    String? ok,
    Color? cancelColor,
    Color? okColor,
    Function? cancelFun,
    Function? okFun,
  }) {
    barrierDismissible = barrierDismissible ?? true;
    showCupertinoDialog<void>(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (BuildContext context){
        Widget widget = CupertinoAlertDialog(
          title: title != null && title.isNotEmpty
              ? Text(
            title,
            style: TextStyle(
              color:  ColorConfig.colorBlack,
              fontSize: 18.sp,
            ),
          )
              : null,
          content: contentWidget ??
              (content != null && content.isNotEmpty
                  ? Text(
                content,
                style: TextStyle(
                  color: ColorConfig.colorBlack,
                  fontSize: 16.sp,
                ),
              )
                  : null),
          actions:
          cancel != null && cancel.isNotEmpty && ok != null && ok.isNotEmpty
              ? [
            CupertinoDialogAction(
              onPressed: () {
                if (cancelFun != null) {
                  cancelFun();
                } else {
                  Get.back();
                }
              },
              child: Text(
                cancel,
                style: TextStyle(
                  color: cancelColor ?? ColorConfig.colorBlue,
                  fontSize: 16.sp,
                ),
              ),
            ),
            CupertinoDialogAction(
              onPressed: () {
                if (okFun != null) {
                  okFun();
                } else {
                  Get.back();
                }
              },
              child: Text(
                ok,
                style: TextStyle(
                  color: okColor ?? ColorConfig.colorBlue,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ]
              : cancel != null && cancel.isNotEmpty
              ? [
            CupertinoDialogAction(
              onPressed: () {
                if (cancelFun != null) {
                  cancelFun();
                } else {
                  Get.back();
                }
              },
              child: Text(
                cancel,
                style: TextStyle(
                  color: cancelColor ?? ColorConfig.colorBlue,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ]
              : ok != null && ok.isNotEmpty
              ? [
            CupertinoDialogAction(
              onPressed: () {
                if (okFun != null) {
                  okFun();
                } else {
                  Get.back();
                }
              },
              child: Text(
                ok,
                style: TextStyle(
                  color: okColor ?? ColorConfig.colorBlue,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ]
              : [],
        );
        if (barrierDismissible == true) {
          return widget;
        }else {
          return WillPopScope(
              onWillPop: ()=>Future.value(false),
              child:widget
          );
        }
      },
    );
  }

  static alertDialog(
    BuildContext context, {
    String? title,
    Widget? contentWidget,
    String? content,
    bool? barrierDismissible,
    EdgeInsets? contentPadding,
    String? cancel,
    String? ok,
    Color? cancelColor,
    Color? okColor,
    Function? cancelFun,
    Function? okFun,
  }) {
    barrierDismissible = barrierDismissible ?? true;
    showDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (BuildContext context) {
        Widget widget =  AlertDialog(
          title: title != null && title.isNotEmpty
              ? Text(
            title,
            style: TextStyle(
              color: ColorConfig.colorBlack,
              fontSize: 18.sp,
            ),
          )
              : null,
          content: contentWidget ??
              (content != null && content.isNotEmpty
                  ? Text(
                content,
                style: TextStyle(
                  color: ColorConfig.colorBlack,
                  fontSize: 16.sp,
                ),
              )
                  : null),
          contentPadding: contentPadding ??
              const EdgeInsets.fromLTRB(24.0, 20.0, 24.0, 24.0),
          actions:
          cancel != null && cancel.isNotEmpty && ok != null && ok.isNotEmpty
              ? [
            ButtonWidget(
              onPressed: () {
                if (cancelFun != null) {
                  cancelFun();
                } else {
                  Get.back();
                }
              },
              padding: EdgeInsets.symmetric(
                vertical: 10.w,
                horizontal: 10.w,
              ),
              child: Text(
                cancel,
                style: TextStyle(
                  color: cancelColor ?? ColorConfig.colorBlack,
                  fontSize: 16.sp,
                ),
              ),
            ),
            ButtonWidget(
              onPressed: () {
                if (okFun != null) {
                  okFun();
                } else {
                  Get.back();
                }
              },
              padding: EdgeInsets.symmetric(
                vertical: 10.w,
                horizontal: 10.w,
              ),
              child: Text(
                ok,
                style: TextStyle(
                  color: okColor ?? ColorConfig.colorPrimary,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ]
              : cancel != null && cancel.isNotEmpty
              ? [
            ButtonWidget(
              onPressed: () {
                if (cancelFun != null) {
                  cancelFun();
                } else {
                  Get.back();
                }
              },
              padding: EdgeInsets.symmetric(
                vertical: 10.w,
                horizontal: 10.w,
              ),
              child: Text(
                cancel,
                style: TextStyle(
                  color: cancelColor ?? ColorConfig.colorBlack,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ]
              : ok != null && ok.isNotEmpty
              ? [
            ButtonWidget(
              onPressed: () {
                if (okFun != null) {
                  okFun();
                } else {
                  Get.back();
                }
              },
              padding: EdgeInsets.symmetric(
                vertical: 10.w,
                horizontal: 10.w,
              ),
              child: Text(
                ok,
                style: TextStyle(
                  color: okColor ?? ColorConfig.colorPrimary,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ]
              : [],
        );
        if (barrierDismissible == true) {
          return widget;
        }else {
          return WillPopScope(
              onWillPop: ()=>Future.value(false),
              child:widget
          );
        }
      },
    );
  }

  static simpleDialog(
    BuildContext context, {
    String? title,
    Widget? contentWidget,
    String? content,
        bool? barrierDismissible,
    EdgeInsets? contentPadding,
    String? cancel,
    String? ok,
    Color? cancelColor,
    Color? okColor,
    Function? cancelFun,
    Function? okFun,
  }) {
    barrierDismissible = barrierDismissible ?? true;
    showDialog(
      context: context,
      barrierDismissible: barrierDismissible,
      builder: (BuildContext context) {
        return SimpleDialog(
          title: title != null && title.isNotEmpty
              ? Text(
                  title,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: ColorConfig.colorBlack,
                    fontSize: 18.sp,
                  ),
                )
              : null,
          titlePadding: EdgeInsets.fromLTRB(15.w, 15.w, 15.w, 0),
          contentPadding: EdgeInsets.zero,
          children: [
            Offstage(
              offstage: content == null || content.isEmpty,
              child: Container(
                margin: EdgeInsets.fromLTRB(
                  contentPadding != null ? contentPadding.left : 15.w,
                  contentPadding != null ? contentPadding.top : 15.w,
                  contentPadding != null ? contentPadding.right : 15.w,
                  contentPadding != null ? contentPadding.bottom : 15.w,
                ),
                constraints: BoxConstraints(minHeight: 60.w),
                alignment: Alignment.center,
                child: Text(
                  content ?? "",
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    color: ColorConfig.colorBlack,
                    fontSize: 16.sp,
                  ),
                ),
              ),
            ),
            Offstage(
              offstage: contentWidget == null,
              child: Padding(
                padding: EdgeInsets.fromLTRB(
                  contentPadding != null ? contentPadding.left : 15.w,
                  contentPadding != null ? contentPadding.top : 15.w,
                  contentPadding != null ? contentPadding.right : 15.w,
                  contentPadding != null ? contentPadding.bottom : 15.w,
                ),
                child: contentWidget,
              ),
            ),
            Offstage(
              offstage: !(content != null && content.isNotEmpty ||
                  contentWidget != null),
              child: Container(
                color: const Color(0xffe0e0e0),
                height: 1,
              ),
            ),
            Flex(
              direction: Axis.horizontal,
              children: cancel != null &&
                      cancel.isNotEmpty &&
                      ok != null &&
                      ok.isNotEmpty
                  ? [
                      Flexible(
                        fit: FlexFit.tight,
                        child: ButtonWidget(
                          onPressed: () {
                            if (cancelFun != null) {
                              cancelFun();
                            } else {
                              Get.back();
                            }
                          },
                          padding: EdgeInsets.symmetric(vertical: 10.w),
                          child: Text(
                            cancel,
                            style: TextStyle(
                              color: cancelColor ?? ColorConfig.colorBlack,
                              fontSize: 16.sp,
                            ),
                          ),
                        ),
                      ),
                      Container(
                        color: const Color(0xffe0e0e0),
                        width: 1,
                        height: 30,
                      ),
                      Flexible(
                        fit: FlexFit.tight,
                        child: ButtonWidget(
                          onPressed: () {
                            if (okFun != null) {
                              okFun();
                            } else {
                              Get.back();
                            }
                          },
                          padding: EdgeInsets.symmetric(vertical: 10.w),
                          child: Text(
                            ok,
                            style: TextStyle(
                              color: okColor ?? ColorConfig.colorPrimary,
                              fontSize: 16.sp,
                            ),
                          ),
                        ),
                      ),
                    ]
                  : cancel != null && cancel.isNotEmpty
                      ? [
                          Flexible(
                            fit: FlexFit.tight,
                            child: ButtonWidget(
                              onPressed: () {
                                if (cancelFun != null) {
                                  cancelFun();
                                } else {
                                  Get.back();
                                }
                              },
                              padding: EdgeInsets.symmetric(vertical: 10.w),
                              child: Text(
                                cancel,
                                style: TextStyle(
                                  color: cancelColor ?? ColorConfig.colorBlack,
                                  fontSize: 16.sp,
                                ),
                              ),
                            ),
                          ),
                        ]
                      : ok != null && ok.isNotEmpty
                          ? [
                              Flexible(
                                fit: FlexFit.tight,
                                child: ButtonWidget(
                                  onPressed: () {
                                    if (okFun != null) {
                                      okFun();
                                    } else {
                                      Get.back();
                                    }
                                  },
                                  padding: EdgeInsets.symmetric(vertical: 10.w),
                                  child: Text(
                                    ok,
                                    style: TextStyle(
                                      color: okColor ?? ColorConfig.colorPrimary,
                                      fontSize: 16.sp,
                                    ),
                                  ),
                                ),
                              ),
                            ]
                          : [],
            ),
          ],
        );
      },
    );
  }

  static customDialog(
    BuildContext context, {
    double? blurSigmaX,
    double? blurSigmaY,
    String? title,
    String? content,
    String? cancel,
    String? ok,
    Color? cancelColor,
    Color? okColor,
    Function? cancelFun,
    Function? okFun,
  }) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        bool isLandscape = Get.mediaQuery.orientation == Orientation.landscape;
        return ScreenUtilInit(
          designSize: isLandscape
              ? const Size(designHeight, designWidth)
              : const Size(designWidth, designHeight),
          builder: (BuildContext context, Widget? child) {
            return GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: Container(
                width: double.infinity,
                height: double.infinity,
                color: Colors.transparent,
                child: BackdropFilter(
                  //背景滤镜
                  filter: ImageFilter.blur(
                      sigmaX: blurSigmaX ?? 5, sigmaY: blurSigmaY ?? 5),
                  child: Center(
                    child: GestureDetector(
                      onTap: () {
                        //do nothing
                      },
                      child: Container(
                        width: 315.w,
                        padding: EdgeInsets.fromLTRB(0, 25.w, 0, 25.w),
                        decoration: BoxDecoration(
                          color: const Color(0xffFFFFFF),
                          borderRadius: BorderRadius.circular(15.w),
                        ),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Padding(
                              padding: EdgeInsets.fromLTRB(25.w, 0, 25.w, 0),
                              child: Text(
                                title ?? "",
                                style: TextStyle(
                                  color: ColorConfig.colorBlack,
                                  fontSize: 18.sp,
                                ),
                              ),
                            ),
                            Offstage(
                              offstage: content == null || content.isEmpty,
                              child: Padding(
                                padding:
                                    EdgeInsets.fromLTRB(25.w, 15.w, 25.w, 0),
                                child: Container(
                                  constraints: BoxConstraints(
                                    minHeight: 60.w,
                                  ),
                                  child: Text(
                                    content ?? "",
                                    style: TextStyle(
                                      color: ColorConfig.colorBlack,
                                      fontSize: 16.sp,
                                    ),
                                  ),
                                ),
                              ),
                            ),
                            Offstage(
                              offstage: !(cancel != null && cancel.isNotEmpty ||
                                  ok != null && ok.isNotEmpty),
                              child: SizedBox(height: 15.w),
                            ),
                            Row(
                              mainAxisSize: MainAxisSize.min,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Offstage(
                                  offstage: cancel == null || cancel.isEmpty,
                                  child: ButtonWidget(
                                    onPressed: () {
                                      if (cancelFun != null) {
                                        cancelFun();
                                      } else {
                                        Get.back();
                                      }
                                    },
                                    child: Container(
                                      width: 115.w,
                                      height: 46.w,
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                        border: Border.all(
                                          color: Colors.grey,
                                          width: 1,
                                        ),
                                        borderRadius:
                                            BorderRadius.circular(46.w),
                                      ),
                                      child: Text(
                                        cancel ?? "",
                                        style: TextStyle(
                                          color: cancelColor ??
                                              const Color(0xff4D4D4D),
                                          fontSize: 16.sp,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                                Offstage(
                                  offstage:
                                      !(cancel != null && cancel.isNotEmpty ||
                                          ok != null && ok.isNotEmpty),
                                  child: SizedBox(width: 20.w),
                                ),
                                Offstage(
                                  offstage: ok == null || ok.isEmpty,
                                  child: ButtonWidget(
                                    onPressed: () {
                                      if (okFun != null) {
                                        okFun();
                                      } else {
                                        Get.back();
                                      }
                                    },
                                    child: Container(
                                      width: 115.w,
                                      height: 46.w,
                                      alignment: Alignment.center,
                                      decoration: BoxDecoration(
                                        color: const Color(0xffF37021),
                                        borderRadius:
                                            BorderRadius.circular(46.w),
                                      ),
                                      child: Text(
                                        ok ?? "",
                                        style: TextStyle(
                                          color: okColor ??
                                              const Color(0xffFFFFFF),
                                          fontSize: 16.sp,
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }

  static loadingDialog(
    BuildContext context, {
    String? content,
    double? blurSigmaX,
    double? blurSigmaY,
    bool? barrierDismissible,
  }) {
    showDialog(
      context: context,
      barrierDismissible: barrierDismissible ?? false,
      builder: (BuildContext context) {
        bool isLandscape = Get.mediaQuery.orientation == Orientation.landscape;
        return ScreenUtilInit(
          designSize: isLandscape
              ? const Size(designHeight, designWidth)
              : const Size(designWidth, designHeight),
          builder: (BuildContext context, Widget? child) {
            return GestureDetector(
              onTap: () {
                if (barrierDismissible ?? false) {
                  Navigator.pop(context);
                }
              },
              child: Container(
                color: Colors.transparent,
                width: double.infinity,
                height: double.infinity,
                child: BackdropFilter(
                  //背景滤镜
                  filter: ImageFilter.blur(
                      sigmaX: blurSigmaX ?? 5, sigmaY: blurSigmaY ?? 5),
                  child: Center(
                    child: GestureDetector(
                      onTap: () {
                        //do nothing
                      },
                      child: Container(
                        width: 202.w,
                        height: 202.w,
                        padding: EdgeInsets.all(15.w),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                          color: const Color(0x66000000),
                          borderRadius: BorderRadius.circular(15.w),
                        ),
                        child: Column(
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            CupertinoActivityIndicator(
                              color: const Color(0xffF37021),
                              radius: 35.w,
                            ),
                            SizedBox(height: 15.w),
                            Text(
                              content ?? "loading".tr,
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                              style: TextStyle(
                                color: const Color(0xffFFFFFF),
                                fontSize: 18.sp,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }
}
